package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("文件上传：{},{},{}",username,age,image);
        String originalFilename = image.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);
        String newFileName= UUID.randomUUID().toString()+extname;
        log.info("新文件名：{}",newFileName);
        image.transferTo(new File("E://images//"+newFileName));
        return Result.success();

    }*/
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    public Result upload(@RequestParam("image") MultipartFile image) throws IOException {
        log.info("文件上传：{}",image.getOriginalFilename());
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
    @PostMapping("/upload1")
    public Result upload1(@RequestParam("dataset") MultipartFile dataset) throws IOException {
        log.info("文件上传：{}",dataset.getOriginalFilename());
        String url = aliOSSUtils.upload(dataset);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
}
